# coding:utf-8
from typing import List, Optional

from fastapi import APIRouter, Depends, Query
from tortoise.expressions import Q

from app.admin.schemas.user import UserOutDB
from app.admin.service.user import UserService
from app.core.auth import DependAuth
from app.core.response import unify_response, SuccessExtra

router = APIRouter()


@router.get("/list")
async def page_list(
        page: int = Query(1, description="页码"),
        limit: int = Query(10, description="每页数量"),
        username: str = Query(None, description="用户名称，用于搜索"),
        status: Optional[str] = Query(None, description="用户状态"),

):
    # 查询条件
    # 查询条件
    query = Q()
    if username:
        query &= Q(username__contains=username)
    if status and int(status) in [0, 1, 2]:
        query &= Q(status__lt=status)
    model_service = UserService()
    total, lists = await model_service.page_list(query, page, limit)
    data = [await obj.to_dict(m2m=True, exclude_fields=["password"]) for obj in lists]
    return SuccessExtra(data=data, total=total, page=page, limit=limit)

# @router.post("/register", summary="注册用户")
# async def register(admin: AdminRegister):
#     """
#     创建用户
#     :param admin: AdminRegister
#     :return:
#     """
#     admin_service = AdminService()
#     # 过滤用户
#     if admin.password != admin.confirm_password:
#         return response_fail(msg="确认密码不一致")
#
#     get_user = await admin_service.find({"username": admin.username})
#     if get_user:
#         return response_fail(msg=f"用户名{admin.username}已经存在!")
#
#     # 创建用户
#     create_user = await admin_service.register(admin)
#     if not create_user:
#         return response_fail(msg=f"用户{admin.username}创建失败!")
#     # if admin.roles:
#     #     # 有分配角色
#     #     roles = await Role.filter(id__in=admin.roles, role_status=True)
#     #     await create_user.role.add(*roles)
#     return response_ok(msg=f"用户{admin.username}注册成功")
